<?php
include 'user_lib.php';

$a_css_files = array (
    array ( 'path' => 'plugins/fancybox/source/jquery.fancybox.css' ),
);

$a_js_files = array (
    array ( 'path' => 'plugins/fancybox/source/jquery.fancybox.pack.js' ),
    array ( 'path' => 'scripts/nh-user-list.js' ),
);

$a_js_class[] = 'User_List.init();';

$a_user = $a_user_class = array ( );
$s_Where = 'where 1=1 ';
$s_category_course = $s_category_course ? $s_category_course : $a_KhoaHoc_Info['start_year'];

// <editor-fold defaultstate="collapsed" desc="Make Where">
if ($s_category_team || $s_category_level || $s_category_crew || $s_category_course) {
    $db_query = "
        select  userid
        from    user_class
        where   1=1";

    if ($s_category_team        ) { $db_query .= " and team         = '$s_category_team'"       ; }
    if ($s_category_level       ) { $db_query .= " and level        = '$s_category_level'"      ; }
    if ($s_category_crew        ) { $db_query .= " and crew         = '$s_category_crew'"       ; }
    if ($s_category_course      ) { $db_query .= " and course       = '$s_category_course'"     ; }
    
    $db_result = db_query($db_conn, $db_query);

    while ($d_row = db_fetch($db_result)) {
        $a_user[$d_row->userid] = $d_row->userid;
    }

    $s_Where = $s_Where  . ' and userid in ("' . implode('", "', $a_user) . '")';

    $a_user = array();
}

make_where($s_Where, $a_filterings);
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Get Data">
$db_query = "select count(userid) from user $s_Where and $s_get_valid_user_condition limit 1";
$d_total  = db_query_single_value($db_conn, $db_query);

namhoa_pagelink($s_Pagination, $s_ScriptName, $s_QueryString, $i_Page, $i_Page_Size, $s_Page_Start, $d_total);

$db_query  = db_get_query($a_tablecolumns_namhoa_user) . $s_Where . " and $s_get_valid_user_condition order by updatedate desc";
$db_result = db_query_page($db_conn, $db_query, $s_Page_Start, $i_Page_Size);

while ($d_row = db_fetch($db_result)) {
    $a_user[$d_row->userid] = $d_row;
    $a_user_class[$d_row->userid] = $d_row->userid;
}

$a_user_class = get_user_class($a_user_class, array($s_category_course), $db_conn);

foreach ($a_user_class as $key => $a_course) {

    $a_course = array_pop($a_course);

    $a_user[$key]->course = $a_course->course;
    $a_user[$key]->team   = $a_course->team;
    $a_user[$key]->level  = $a_course->level;
    $a_user[$key]->crew   = $a_course->crew;
}

$a_permission = get_passport_action_from_module($db_conn, array('DANH_SACH'));
// </editor-fold>

include 'lib_head.php';
?>
<!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<div id="portlet-config" class="modal hide">
    <div class="modal-header">
        <button data-dismiss="modal" class="close" type="button"></button>
        <h3>Bộ Lọc</h3>
    </div>
    <div class="modal-body">
        Điền thông tin cần thiết để lọc được dữ liệu thích hợp.
    </div>
</div>
<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<div class="row-fluid">
    <!-- BEGIN BORDERED TABLE PORTLET-->
    <div class="portlet box green">
        <div class="portlet-title">
            <div class="caption"><i class="icon-filter"></i>Lọc Dữ Liệu</div>
            <div class="tools">
                <a href="javascript:;" class="collapse"></a>
                <a href="#portlet-config" data-toggle="modal" class="config"></a>
            </div>
        </div>
        <div class="portlet-body">
            <form action="" style="margin: 0" id="form-filter">
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Khóa Học</label>
                        <div class="controls">
                            <select name="category_course" class="span12">
                                <?php 
                                $v_tmp_start_course = $a_KhoaHoc_Info['start_year'] + 1;

                                for ($v_index = $v_tmp_start_course; $v_index > $v_tmp_start_course - 10 ; $v_index--) { ?>
                                    <option <?= $s_category_course == $v_index ? 'selected="selected"' : '' ?> value="<?= $v_index ?>"><?= $v_index ?> - <?= $v_index + 1?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Phân Loại</label>
                        <div class="controls">
                            <select name="category6" class="span12">
                                <option value="">Tất Cả</option>
                                <?php foreach ($a_Loai_TaiKhoan as $v_key => $v_value) { ?>
                                    <option <?= $s_category6 == $v_key ? 'selected="selected"' : '' ?> value="<?= $v_key ?>"><?= $v_value ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Phân Ban</label>
                        <div class="controls">
                            <select name="category7" class="span12">
                                <option value="">Tất Cả</option>
                                <?php foreach ($a_PhanBan as $v_key => $v_value) { ?>
                                    <option <?= $s_category7 == $v_key ? 'selected="selected"' : '' ?> value="<?= $v_key ?>"><?= $v_value ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Ngành</label>
                        <div class="controls">
                            <select name="category_team" class="span12">
                                <option value="">Tất Cả</option>
                                <?php foreach ($a_Nganh as $v_key => $v_value) { ?>
                                    <option <?= $s_category_team == $v_key ? 'selected="selected"' : '' ?> value="<?= $v_key ?>"><?= $v_value ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Cấp</label>
                        <div class="controls">
                            <select name="category_level" class="span12">
                                <option value="">Tất Cả</option>
                                <?php foreach ($a_Cap as $v_key => $v_value) { ?>
                                    <option <?= $s_category_level == $v_key ? 'selected="selected"' : '' ?> value="<?= $v_key ?>"><?= $v_value ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Đội</label>
                        <div class="controls">
                            <select name="category_crew" class="span12">
                                <option value="">Tất Cả</option>
                                <?php foreach ($a_Doi as $v_key => $v_value) { ?>
                                    <option <?= $s_category_crew == $v_key ? 'selected="selected"' : '' ?> value="<?= $v_key ?>"><?= $v_value ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left">
                    <div class="control-group">
                        <label class="control-label">Lọc Theo</label>
                        <div class="controls">
                            <div class="input-prepend">
                                <div class="btn-group">
                                    <select name="field1" class="span12">
                                        <?php build_advanced_filter_namhoa_user($s_field1, $a_hide_filter); ?>
                                    </select>
                                </div>
                                <input class="medium span6" name="keyword1" type="text" value="<?=check_plain($s_keyword1)?>"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="clearfix">
                    <button class="btn blue btn-block">BẮT ĐẦU LỌC <i class="m-icon-swapright m-icon-white"></i></button>
                </div>
            </form>
        </div>
    </div>
    <!-- END BORDERED TABLE PORTLET-->
    <div class="portlet-body">
        <table class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>Ảnh</th>
                    <th class="hidden-phone">Mã Số</th>
                    <th class="hidden-phone">Tên Thánh</th>
                    <th>Họ Và Tên</th>
                    <th class="hidden-phone">Phân Loại</th>
                    <th class="hidden-phone">Lớp</th>
                    <th class="hidden-phone">Ngày Sinh</th>
                    <th class="hidden-phone">ĐTDĐ</th>
                    <th class="hidden-phone">ĐT Nhà</th>
                    <th>
                        <a class="btn green btn-export"><i class="icon-file-text"></i> Lưu Excel</a>
                    </th>
                </tr>
            </thead>
        <?php
        foreach ($a_user as $v_counter => $d_user) {
            $v_tmp_usertype = isset($a_Loai_TaiKhoan[$d_user->usertype]) ? $a_Loai_TaiKhoan[$d_user->usertype] : '';
            
            $v_tmp_class  = $d_user->course;
            $v_tmp_class .= isset($a_Nganh [$d_user->team ]) ? '   ' . $a_Nganh [$d_user->team ] : '';
            $v_tmp_class .= isset($a_Cap[$d_user->level]) ? ' - ' . $a_Cap[$d_user->level] : '';
            $v_tmp_class .= isset($a_Doi [$d_user->crew ]) ? ' - ' . $a_Doi [$d_user->crew ] : '';
            ?>
            <tbody>
                <tr>
                    <td>
                        <div class="item">
                            <a class="fancybox-button" data-rel="fancybox-button" title="<?php echo "$d_user->saint $d_user->username - Lớp $v_tmp_class"?>" href="user_image.php?action=download-image&amp;userid=<?= $d_user->userid ?>&image.jpg">
                                <div class="zoom">
                                    <img src="user_image.php?action=download-image&amp;size=60&amp;userid=<?= $d_user->userid ?>" alt="<?php echo "$d_user->saint $d_user->username - Lớp $v_tmp_class"?>" />                    
                                    <div class="zoom-icon" style="width: 60px; height: 60px"></div>
                                </div>
                            </a>
                        </div>
                    </td>
                    <td class="hidden-phone"><?=$d_user->userid?></td>
                    <td class="hidden-phone"><?=check_plain($d_user->saint)?></td>
                    <td>
                        <a href="user_detail.php?userid=<?=$d_user->userid?>">
                            <?=check_plain($d_user->username)?>
                        </a>
                    </td>
                    <td class="hidden-phone"><?=$v_tmp_usertype?></td>
                    <td class="hidden-phone">
                        <?php if($v_tmp_class) : ?>
                        <a href="user_detail_diary.php?userid=<?=$d_user->userid?>&course=<?=$d_user->course?>"><?= $v_tmp_class ?></a>
                        <?php endif; ?>
                    </td>
                    <td class="hidden-phone"><?=namhoa_date($d_user->birthday)?></td>
                    <td class="hidden-phone"><?=check_plain($d_user->mobile)?></td>
                    <td class="hidden-phone"><?=check_plain($d_user->phone)?></td>
                    <td>
                        <div class="btn-group">
                            <a data-toggle="dropdown" href="#" class="btn purple">
                                <i class="icon-cogs"></i> Thao Tác
                                <i class="icon-angle-down"></i>
                            </a>
                            <ul class="dropdown-menu">
                                <li><a href="user_detail.php?userid=<?=$d_user->userid?>"><i class="icon-user"></i> Xem / Sửa</a></li>
                                <?php if (in_array('delete-profile', $a_permission)) {?>
                                <li><a href="javascript://" onclick="namhoa.single_delete('user_detail_execute.php?action=delete-profile&amp;userid=<?=$d_user->userid?>&amp;<?=$s_QueryString_Page?>');"><i class="icon-trash"></i> Xóa</a></li>
                                <?php } ?>
                                <li class="divider"></li>
                                <li><a href="user_detail_diary.php?userid=<?=$d_user->userid?>&course=<?=$d_user->course?>"><i class="icon-trophy"></i> Thành Tích</a></li>
                            </ul>
                        </div>
                    </td>
                </tr>
            </tbody>
        <?php } ?>
        </table>
    </div>
    <div class="pagination pagination-centered">
        <?=$s_Pagination?>
    </div>
</div>
<?php include 'lib_tail.php'; ?>